
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var wxAuthFail = true; 
var inviteuid = 0;
Page({
  data: {
    util: util,
    tc: 0,

    bottom_nav_data:[],
    isIphone_x: false,
    nav_bgColor: '#ffffff',
    
    token: '',
    uid: 0,
    userInfo:{},
    user_jifen: 0,
    showLogin:true,

    colorSet: '',
    coin:0
  },
  onLoad: function(data){
    
    var that = this;
    var tc = app.get_tc();//简体繁体转换
    this.setData({
      tc: tc,
    });
    inviteuid = data.inviteuid ? data.inviteuid : '';
    

    //获取关于等信息
    app.getAllAbout('contact', function (res) {
      that.setData({
        contact_phone: res
      })
    });
    app.getAllAbout('shouhou', function (res) {
      if(res && res.length > 0){
        that.setData({
          aboutMessage: res
        });
      }
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('mine/mine', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });


    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });
    var titleName = tc == 1 ? '我' : '我';

    wx.setNavigationBarTitle({
      title: titleName,
    })

  },
  onShow:function(){
    this.getLoginInfo();
    wx.login({
      success: function (e) {
        console.log("获取到登录凭证code：", e.code);
        wx.setStorageSync('code', e.code);
      },
      fail: function (e) {
        console.log("wx.login fail:", e);
      }
    });
    var membercard = wx.getStorageSync('membercard');
    if(membercard){
      this.setData({
        showLogin: false
      })
    }else{
      this.setData({
        showLogin: true
      })
    }
  },

  getYuE:function(){
    var that = this;
    var params = {
      "uid": that.data.userInfo.uid,
      "publickey": util.publiccode,
      "ac": "coininfo",
      "privatekey": utilMd5.hexMD5(that.data.userInfo.uid + 'coininfo' + that.data.userInfo.token),
      "tc": that.data.tc
    };
    console.log("余额参数：uid=" + that.data.userInfo.uid + "; token = " + that.data.userInfo.token, params);
    util.api({
      method: "post",
      data: params,
      success: function (responseData) {
        console.log("余额返回：", responseData);
        that.setData({
          coin: (responseData.data.coin || 0) / 100
        });
      }
    });
  },


  getJifen: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getusersource",
        "uid": that.data.uid
      },
      success: function (responseData) {
        that.setData({
          user_jifen: responseData.data.source
        })
      }
    });
  },

  getLoginInfo: function(){
    var that = this;
    app.getLoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      wxAuthFail = false;
        that.setData({
          uid: res.uid,
          token: res.token,
          userInfo: res
        });
        that.getJifen();
        that.getYuE();
      
    });
  },


  goAtherPage: function(e){
    console.log("goAtherPage",e)
    var that = this;
    if (wxAuthFail) {
      
      wx.getUserProfile({
        desc: "下单需要",
        success: function (res) {
          console.log("用户已授权：", res);
          app.wxAuthRequest(res, function (res) {
            if (res != -1) {
              that.getLoginInfo();
            }
          });
        },
        fail: function (err) {
          console.log("wx.getUserInfo fail:", err);
        }
      })
    } else {
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({ url: url })
    }
  },
  goAtherPageRedirect: function(e){
      var url = e.currentTarget.dataset.link;
      wx.redirectTo({url: url})
  },

  //点击登入按钮
  dengru:function(e){
    var that = this;
    if (wxAuthFail) {
      wx.getUserProfile({
        desc: "下单需要",
        success: function (res) {
          console.log("用户已授权：", res);
          app.wxAuthRequest(res, function (res) {
            if (res != -1) {
              that.getLoginInfo();
            }
          });
        },
        fail: function (err) {
          console.log("wx.getUserInfo fail:", err);
        }
      })
    } else {
      var membercard = this.data.userInfo.mobile;
      console.log(membercard);
      if (!membercard) {
        // wx.navigateTo({
        //   url: '/pages/join/join?',
        // })
        wx.navigateTo({
          url: '/pages/login/login?mine=1',
        })
      } else {
        wx.navigateTo({
          url: '/pages/login/login?mine=1&membercard=' + membercard,
        })
      }
    }
    
  },



  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      var navPath = path.slice(6);
      wx.navigateTo({
        url: navPath,
      })
    } else {
      wx.redirectTo({ url: path });
    }
  },



  //页面分享
  onShareAppMessage: function () {
    var invUid = this.data.uid ? this.data.uid : '';

    var path = '/pages/mine/mine?inviteuid=' + invUid;
    console.log("分享给好友：" + path);
     if (util.orgid == "21398") {
      return {
        title: this.data.tc == 1 ? '尚品高質凍肉海產零售批發小程序開通啦，歡迎下單購買！！！':'尚品高质冻肉海产零售批发小程序开通啦，欢迎下单购买！！！',
        path: path
      };
    } else{
      return {
        title: '',
        path: path
      };
    }
    
  },


  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },


  //退出登录
  logoutAction:function(){
    // wx.setStorageSync('membercard', '');
    var that = this;
    wx.clearStorageSync('membercard');
    wx.showLoading({
      title: 'loading',
    })

    setTimeout(function () {
      wx.hideLoading();
      that.setData({
        showLogin: true
      })
      wx.showToast({
        title: '成功退出',
        icon: 'none',
        duration: 1000,
      })  
    }, 1000);
    
  }

})